﻿namespace OffLineVoice
{
    public class TestDemo
    {
        SpeechRecognitionManager speechRecognitionManager;
        Dictionary<string, ISpeakData> keyWordCollection;
        public void Run()
        {
            var builder = new KeyWordBuilder()
                .AddOperatePath()
                .AddToothSelection()
                .AddDentalPositionSelection()
                .AddToothType()
                .AddPocketDepth()
                .AddShrinkageProliferation()
                .AddBifurcation()
                .AddLooseness()
                .AddSymptom()
                .Builder();
            keyWordCollection = builder.KeyWordCollection;
            speechRecognitionManager = new SpeechRecognitionManager();
            speechRecognitionManager.StartspeechSynthesizer();
            speechRecognitionManager.SpeechRecognizedEventHandler += Speech_SpeechRecognizedEventHandler;
            speechRecognitionManager.SpeechRecognitionRejectedEventHandler += Speech_SpeechRecognitionRejectedEventHandler;
            speechRecognitionManager.StartVoiceRecognition(builder.KeyWordCollection.Keys.ToArray());
            speechRecognitionManager.Speak("语音输入已开启");
        }

        private void Speech_SpeechRecognitionRejectedEventHandler(string speechText)
        {
            Console.WriteLine($"{speechText}");
            speechRecognitionManager.Speak(speechText);
        }

        private void Speech_SpeechRecognizedEventHandler(string speechText)
        {
            Console.WriteLine($"检测到语音输入：{speechText}");
            speechRecognitionManager.Speak(speechText);
            AnalyzeKeyWord(speechText, keyWordCollection);
        }

        private void AnalyzeKeyWord(string keyWord, Dictionary<string, ISpeakData> keyWordCollection)
        {
            if (keyWordCollection.Keys.FirstOrDefault(x => x == keyWord) == null) return;
            ISpeakData speakData = keyWordCollection[keyWord];
            switch (speakData.CurrentCommand)
            {
                case CommandType.OperatePath:
                    OperatePathHandler((OperatePathData)speakData);
                    break;
                case CommandType.ToothSelection:
                    ToothSelectionHandler((ToothSelectionData)speakData);
                    break;
                case CommandType.DentalPositionSelection:
                    DentalPositionSelectionHandler((DentalPositionSelectionData)speakData);
                    break;
                case CommandType.ToothType:
                    ToothTypeHandler((ToothTypeData)speakData);
                    break;
                case CommandType.PocketDepth:
                    PocketDepthHandler((PocketDepthData)speakData);
                    break;
                case CommandType.ShrinkageProliferation:
                    ShrinkageProliferationHandler((ShrinkageProliferationData)speakData);
                    break;
                case CommandType.Bifurcation:
                    BifurcationHandler((BifurcationData)speakData);
                    break;
                case CommandType.Looseness:
                    LoosenessHandler((LoosenessData)speakData);
                    break;
                case CommandType.Symptom:
                    SymptomHandler((SymptomData)speakData);
                    break;
                default:
                    break;
            }
        }

        private void OperatePathHandler(OperatePathData speakData)
        {
            speechRecognitionManager.Speak($"{(speakData.OperatePath == 0 ? "前进" : "后退")}操作");
        }

        private void ToothSelectionHandler(ToothSelectionData speakData)
        {
            speechRecognitionManager.Speak($"牙位{speakData.TeethPosition}{(speakData.IsLabial ? "唇侧" : "腭侧")}操作");
        }

        private void DentalPositionSelectionHandler(DentalPositionSelectionData speakData)
        {
            string text = speakData.Position == 0 ? "近中" : speakData.Position == 1 ? "正中" : "远中";
            speechRecognitionManager.Speak($"{text}操作");
        }

        private void ToothTypeHandler(ToothTypeData speakData)
        {
            string type = speakData.ToothType switch
            {
                1 => "桥基牙",
                2 => "桥基牙种植",
                3 => "桥义齿",
                4 => "乳牙",
                5 => "牙冠",
                6 => "龋齿",
                7 => "阻生齿",
                8 => "种植体",
                9 => "缺失牙",
                10 => "恒牙",
                _ => "空类型"
            };
            speechRecognitionManager.Speak($"设置{type}");
        }

        private void PocketDepthHandler(PocketDepthData speakData)
        {
            speechRecognitionManager.Speak($"设置袋深{speakData.Depth}");
        }

        private void ShrinkageProliferationHandler(ShrinkageProliferationData speakData)
        {
            speechRecognitionManager.Speak($"设置{(speakData.Type == 0 ? "退缩" : "增生")}{speakData.Value}");
        }

        private void BifurcationHandler(BifurcationData speakData)
        {
            speechRecognitionManager.Speak($"设置分叉{speakData.Value}");
        }

        private void LoosenessHandler(LoosenessData speakData)
        {
            speechRecognitionManager.Speak($"设置{speakData.Value}级松动度");
        }

        private void SymptomHandler(SymptomData speakData)
        {
            string text = speakData.Type switch
            {
                1 => "菌斑",
                2 => "出血",
                3 => "化脓",
                4 => "出血化脓",
                _ => ""
            };
            speechRecognitionManager.Speak($"设置{text}");
        }
    }
}
